package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.bo.ErpSellerStoreProductExpandBo;
import com.erp.gather.domain.vo.ErpSellerStoreProductExpandVo;
import com.erp.gather.mapper.ErpSellerStoreProductExpandMapper;
import com.erp.gather.service.IErpSellerStoreProductExpandService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpSellerStoreProduct;
import com.ruoyi.gather.api.domain.ErpSellerStoreProductExpand;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 卖家在线商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）Service业务层处理
 *
 * @author erp
 * @date 2023-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreProductExpandServiceImpl implements IErpSellerStoreProductExpandService {

    private final ErpSellerStoreProductExpandMapper baseMapper;

    /**
     * 查询卖家在线商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public ErpSellerStoreProductExpandVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询卖家在线商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreProductExpandVo> queryPageList(ErpSellerStoreProductExpandBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreProductExpand> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreProductExpandVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询卖家在线商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）列表
     */
    @Override
    public List<ErpSellerStoreProductExpandVo> queryList(ErpSellerStoreProductExpandBo bo) {
        LambdaQueryWrapper<ErpSellerStoreProductExpand> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreProductExpand> buildQueryWrapper(ErpSellerStoreProductExpandBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreProductExpand> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSellerStoreProductId() != null, ErpSellerStoreProductExpand::getSellerStoreProductId, bo.getSellerStoreProductId());
        lqw.eq(bo.getAeProductId() != null, ErpSellerStoreProductExpand::getAeProductId, bo.getAeProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getCouponStartDate()), ErpSellerStoreProductExpand::getCouponStartDate, bo.getCouponStartDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCouponEndDate()), ErpSellerStoreProductExpand::getCouponEndDate, bo.getCouponEndDate());
        lqw.eq(bo.getFreightTemplateId() != null, ErpSellerStoreProductExpand::getFreightTemplateId, bo.getFreightTemplateId());
        lqw.eq(bo.getGroupId() != null, ErpSellerStoreProductExpand::getGroupId, bo.getGroupId());
        lqw.eq(StringUtils.isNotBlank(bo.getPriceAdjustmentMethod()), ErpSellerStoreProductExpand::getPriceAdjustmentMethod, bo.getPriceAdjustmentMethod());
        lqw.eq(bo.getReadjustPrices() != null, ErpSellerStoreProductExpand::getReadjustPrices, bo.getReadjustPrices());
        lqw.eq(bo.getPrice() != null, ErpSellerStoreProductExpand::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getAbsoluteQuoteMap()), ErpSellerStoreProductExpand::getAbsoluteQuoteMap, bo.getAbsoluteQuoteMap());
        lqw.eq(StringUtils.isNotBlank(bo.getProductStatusType()), ErpSellerStoreProductExpand::getProductStatusType, bo.getProductStatusType());
        lqw.eq(bo.getPromiseTemplateId() != null, ErpSellerStoreProductExpand::getPromiseTemplateId, bo.getPromiseTemplateId());
        lqw.eq(StringUtils.isNotBlank(bo.getReduceStrategy()), ErpSellerStoreProductExpand::getReduceStrategy, bo.getReduceStrategy());
        lqw.eq(StringUtils.isNotBlank(bo.getSrc()), ErpSellerStoreProductExpand::getSrc, bo.getSrc());
        lqw.eq(StringUtils.isNotBlank(bo.getWsDisplay()), ErpSellerStoreProductExpand::getWsDisplay, bo.getWsDisplay());
        lqw.eq(StringUtils.isNotBlank(bo.getWsOfflineDate()), ErpSellerStoreProductExpand::getWsOfflineDate, bo.getWsOfflineDate());
        lqw.eq(bo.getWsValidNum() != null, ErpSellerStoreProductExpand::getWsValidNum, bo.getWsValidNum());
        lqw.eq(bo.getAliMemberId() != null, ErpSellerStoreProductExpand::getAliMemberId, bo.getAliMemberId());
        lqw.eq(bo.getMediaId() != null, ErpSellerStoreProductExpand::getMediaId, bo.getMediaId());
        lqw.eq(StringUtils.isNotBlank(bo.getMediaStatus()), ErpSellerStoreProductExpand::getMediaStatus, bo.getMediaStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getMediaType()), ErpSellerStoreProductExpand::getMediaType, bo.getMediaType());
        lqw.eq(StringUtils.isNotBlank(bo.getPosterUrl()), ErpSellerStoreProductExpand::getPosterUrl, bo.getPosterUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getEanCode()), ErpSellerStoreProductExpand::getEanCode, bo.getEanCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAutoTaskId()), ErpSellerStoreProductExpand::getAutoTaskId, bo.getAutoTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreProductExpand::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增卖家在线商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreProductExpandBo bo) {
        ErpSellerStoreProductExpand add = BeanUtil.toBean(bo, ErpSellerStoreProductExpand.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改卖家在线商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreProductExpandBo bo) {
        ErpSellerStoreProductExpand update = BeanUtil.toBean(bo, ErpSellerStoreProductExpand.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreProductExpand entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除卖家在线商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Object> selectSellerStoreProductExpandByLaw(LambdaQueryWrapper law) {
        ErpSellerStoreProductExpand erpSellerStoreProductExpand = baseMapper.selectOne(law);
        if (Objects.isNull(erpSellerStoreProductExpand)) {
            return R.fail("未查到该信息");
        } else {
            return R.ok(erpSellerStoreProductExpand);
        }
    }

    @Override
    public R<Object> updateErpSellerStoreProductExpand(ErpSellerStoreProductExpand erpSellerStoreProductExpand) {
        int i = baseMapper.updateById(erpSellerStoreProductExpand);
        if (i > 1) {
            return R.ok("更新成功");
        } else {
            return R.fail("更新失败");
        }
    }

    @Override
    public R<Object> getErpSellerStoreProductExpandInfo(ErpSellerStoreProductExpand erpSellerStoreProductExpand) {
        ErpSellerStoreProductExpandBo erpSellerStoreProductExpandBo = BeanUtil.copyProperties(erpSellerStoreProductExpand, ErpSellerStoreProductExpandBo.class);
        LambdaQueryWrapper<ErpSellerStoreProductExpand> lqw = buildQueryWrapper(erpSellerStoreProductExpandBo);
        ErpSellerStoreProductExpand expand = baseMapper.selectOne(lqw);
        if (Objects.isNull(expand)) {
            return R.ok(null);
        }else {
            return R.ok(expand);
        }
    }

    @Override
    public R<Object> deleteErpSellerStoreProductExpand(ErpSellerStoreProductExpand erpSellerStoreProductExpand) {
        int i = baseMapper.deleteById(erpSellerStoreProductExpand);
        if (i > 0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }

    @Override
    public void updateModuleCount(List<Long> list) {
        for (Long aeProductId : list) {
            ErpSellerStoreProductExpand productExpand = baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerStoreProductExpand>().eq(ErpSellerStoreProductExpand::getAeProductId, aeProductId));
            if (productExpand == null) {
                continue;
            }
            Integer moduleCount = productExpand.getModuleCount();
            if (moduleCount == null) {
                moduleCount = 0;
            }
            productExpand.setModuleCount(moduleCount + 1);
            baseMapper.updateById(productExpand);
        }
    }


}
